import yaml
import os

from submodules.common.lib.log_ import logger




def yaml_file_to_dict(file_path):
    with open(file_path, 'r', encoding='utf8') as f:
        data = yaml.load(f.read(), Loader=yaml.SafeLoader)
    return data


def dict_to_yaml_file(data, file_path):
    with open(file_path, 'w', encoding='utf8') as f:
        yaml.safe_dump(data, f, allow_unicode=True)


if __name__ == '__main__':
    import os
    from submodules.common.lib.local_paths import project_path

    test_data_dir = os.path.join(project_path, "submodules", "common", "test_data")


    def test_yaml_file_to_dict():
        file_path_yaml = os.path.join(test_data_dir, "yaml_", "test.yaml")
        ret = yaml_file_to_dict(file_path_yaml)
        print(ret)

        file_path_json = os.path.join(test_data_dir, "yaml_", "test.json")
        from submodules.common.lib.json_ import dict_to_json_file
        res = dict_to_json_file(ret, file_path_json)
        print(res)


    def test_dict_to_yaml_file():
        file_path_json = os.path.join(test_data_dir, "yaml_", "test.json")
        file_path_yaml2 = os.path.join(test_data_dir, "yaml_", "test2.yaml")

        from submodules.common.lib.json_ import json_file_to_dict
        res = json_file_to_dict(file_path_json)
        print(res)
        res = dict_to_yaml_file(res, file_path_yaml2)
        print(res)


    test_dict_to_yaml_file()
